<template>
	<view>
		<!-- 头部 -->
		<view class="square-title">
			<view class="square-title-icon">
				<img src="../../static/tv.png" alt="" style="width: 50rpx;height: 50rpx;">
				<view v-if="LinkType">火热新剧</view>
				<view v-else>火热推荐</view>
			</view>
			<text></text>
		</view>
		<!-- 轮播图 -->
		<view class="swiper-dot">
			<uni-swiper-dot :info="bannerList" :current="current" mode="round">
				<swiper class="swiper-box" interval="3000" @change="swiperClick" autoplay="true" :circular="true">
					<swiper-item v-for="(item ,index) in bannerList" :key="index" @click="tovideo(item)">
						<view class="image">
							<image :src="item.cover_img" />
						</view>
					</swiper-item>
				</swiper>
			</uni-swiper-dot>
		</view>
		<!-- 历史观看 -->
		<view class="square-history" v-if="LinkType">
			<!-- <view class="square-title">
				<view class="square-title-icon">
					<img style="width: 40rpx;height: 40rpx;" src="../../static/broadcast.png" alt="">
					<view>上次观看</view>
				</view>
				<text></text>
			</view> -->
			<view class="history-list">
				<view v-for="(item,index) in historyArray" :key="index" class="history-for">
					<view class="history-view" @click="tovideo(item)">
						<image :src="item.cover_img" mode="" lazy-load :lazy-load-margin="0"></image>
						<view class="history-text">
							<view>{{item.name}}</view>
							<text>观看至第{{item.sort}}集 共{{item.count}}集</text>
						</view>
					</view>
					<view v-if="index === 0 && LinkType" class="history-recently">
						上次观看
					</view>
				</view>	
			</view>
		</view>

		<!-- 列表 -->
		<view class="square-list">
			<view class="square-title" style="padding:30rpx 20rpx 30rpx 1rpx;">
				<view class="square-title-icon">
					<img src="../../static/Popular.png" alt="">
					<view v-if="LinkType">热播短剧</view>
					<view v-else>最新推荐</view>
				</view>
				<text>大家都在看</text>
			</view>
			<view class="broadcast">
				<view v-for="(item,index) in historyList" :key="index" @click="tovideo(item)">
					<view class="broadcast-img">
						<img :src="item.cover_img" alt="" lazy-load :lazy-load-margin="0">
					</view>
					<view class="broadcast-title">{{item.name}}</view>
					<text v-if="LinkType">已完结</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getSmallTheaterClass,
		getrecordlist
	} from '../../module/index.js'
	export default {
		data() {
			return {
				bannerList: [],
				historyList: [],
				historyArray: [],
				LinkType: false,
				params: {
					type_id: null,
					page: 1,
					limit: 10,
					total_page: 0
				},
				current: 0,
			}
		},
		onLoad(options) {
			uni.setStorageSync('login_message', {
				official_account_id: options?.official_account_id,
				uid: options?.uid,
				video_id: options?.video_id
			})
		},
		async onShow() {
			await this.$onLaunched
			this.getCateData()
			this.getrecordlist()
			this.LinkType = uni.getStorageSync('is_online')
		},
		onReachBottom() {
			let {
				total_page,
				page
			} = this.params
			if (page > total_page > 0) {
				return;
			}
			this.getCateData() 
		},
		methods: {
			tovideo(item) {
				if (uni.getStorageSync('is_online')) {
					uni.navigateTo({
						url: "/wxcomponents/yjy-video-slide/yjy-video-slide?video_id=" + (item?.count ? item
								.video_id : item.id) +
							'&video_detail_id=' + (item?.count ? item.video_detail_id : item.id)
					});
				} else {
					// uni.navigateTo({
					// 	url: `/pages/recommend/yjy-video-slide/yjy-video-slide?id=${item.id}`
					// })
				}
			},
			swiperClick(e) {
				this.current = e.detail.current;
			},
			getCateData() {
				//发起请求
				getSmallTheaterClass(this.params).then((res) => {
					if (res.code === 200) {
						this.bannerList = res.data.banner
						this.historyList.push(...res.data.recommended.data)
						this.params.page++
						this.params.total_page = res.data.recommended.total_page
					} else {
						uni.showToast({
							title: res.message,
							icon: "none"
						})
					}
				}).catch(err => {
					console.log(err)
				})
			},
			getrecordlist() {
				//发起请求
				getrecordlist().then((res) => {
					if (res.code === 200) {
						this.historyArray = res.data.data
					} else {
						uni.showToast({
							title: res.message,
							icon: "none"
						})
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.square-title {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 20rpx;

		text {
			color: #999aaa;
			font-size: 23rpx
		}
	}

	.square-title-icon {
		display: flex;
		align-items: center;

		img {
			width: 45rpx;
			height: 45rpx;
		}

		view {
			font-size: 32rpx;
			font-weight: 600;
			margin-left: 8rpx;
		}
	}

	.swiper-dot {
		padding: 0 20rpx;
		z-index: 1000;

		.swiper-box {
			height: 300rpx;
		}

		.image {
			width: 100%;
			height: 100%;

			image {
				width: 100%;
				height: 100%;
			}
		}
	}

	.square-history {
		margin: 40rpx 20rpx;
		border-radius: 15rpx;
		margin-top: 40rpx;

		.history-list {
			overflow-y: auto;
			display: flex;


			.history-for {
				margin: 8rpx 20rpx 8rpx 0;
				border-radius: 15rpx;
				box-shadow: 2rpx 2rpx 8rpx 0 rgba(0, 0, 0, 0.1);
				position: relative;

				.history-recently {
					position: absolute;
					top: 0;
					right: 0;
					padding: 8rpx;
					background-color: #FFE4C4;
					color: #d81e06;
					font-weight: 600;
					font-size: 20rpx;
					border-radius: 0 15rpx 0 15rpx;
				}
			}

			.history-view {
				width: 380rpx;
				display: flex;
				align-items: center;
				border-radius: 10rpx;

				image {
					width: 100rpx;
					height: 150rpx;
					border-radius: 10rpx;
				}

				.history-text {
					margin-left: 20rpx;

					>view {
						width: 240rpx;
						font-size: 24rpx;
						font-weight: 800;
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;
						margin-bottom: 10rpx;
					}

					>text {
						font-size: 22rpx;
						color: #999aaa;
					}
				}
			}
		}
		.history-list::-webkit-scrollbar {
			display: none;
			width: 0;
			height: 0;
			color: transparent;
		}
	}

	.square-list {
		background-color: #F5F5F5;
		border-radius: 15rpx;
		margin: 0 20rpx;
		margin-top: 60rpx;
	}

	.broadcast {
		width: 100%;
		display: flex;
		flex-wrap: wrap;

		>view:nth-child(3n+2) {
			margin-left: 5%;
			margin-right: 5%;
		}

		>view {
			width: 30%;
			margin-bottom: 20rpx;
			text-align: center;

			.broadcast-img {
				// width: 200rpx;
				width: 100%;
				height: 320rpx;
				margin: auto;

				img {
					width: 100%;
					height: 100%;
					border-radius: 10rpx;
				}
			}

			>text {
				color: #999;
				font-size: 20rpx;
				margin-top: -10rpx;
			}

			.broadcast-title {
				width: 170rpx;
				padding: 15rpx 30rpx 0 30rpx;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
				font-size: 24rpx;
				font-weight: 600;
			}
		}

	}
</style>